/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package persistence;

import java.io.FileWriter;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import model.Voorstelling;
import org.jdom2.Attribute;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.Namespace;
import org.jdom2.output.Format;
import org.jdom2.output.XMLOutputter;

/**
 *
 * @author Matthy & Jelle
 */
public class XmlService {
    
    public String maakWeekSchema(Date datum){
        try{
        Element programmatie = new Element("programmatie");
        Namespace XSI_NAMESPACE = Namespace.getNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance");
        programmatie.addNamespaceDeclaration(XSI_NAMESPACE);
        programmatie.setAttribute("noNamespaceSchemaLocation","schema.xsd", XSI_NAMESPACE);
        
        Document doc = new Document(programmatie);
        
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(datum);
        calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
        Date weekbegin = calendar.getTime();
        
        calendar.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
        Date weekeinde = calendar.getTime();
        
        List<Voorstelling> voorstellingen;
        VoorstellingService voorstellingService = new VoorstellingService();
        voorstellingen = voorstellingService.getXMLVoorstellingen(weekbegin, weekeinde);
        
        int previouscomplex = 0;
        int previousday = 0;
        int previouszaal = 0;
        
        Element complex = new Element("temp");
        Element zaal = new Element("temp");
        Element dag = new Element("temp");
        
        for(Voorstelling v : voorstellingen){
            if(v.getZaal().getComplex().getId() != previouscomplex){
                if(previouscomplex != 0){
                    doc.getRootElement().addContent(complex);
                }
                complex = new Element("complex");
                complex.setAttribute(new Attribute("locatie", v.getZaal().getComplex().getLocatie()));
                previouscomplex = v.getZaal().getComplex().getId();
            }
            
            calendar.setTime(v.getTijdstip());
            if(calendar.get(Calendar.DAY_OF_YEAR) != previousday){
                dag = new Element("dag");
                DateFormat dateformat = new SimpleDateFormat("dd-MM-yyyy");
                dag.setAttribute(new Attribute("datum", dateformat.format(v.getTijdstip())));
                complex.addContent(dag);
                previousday = calendar.get(Calendar.DAY_OF_YEAR);
            }
            
            if(v.getZaal().getId() != previouszaal){
                zaal = new Element("zaal");
                zaal.setAttribute(new Attribute("nr", v.getZaal().getNr().toString()));
                dag.addContent(zaal);
                previouszaal = v.getZaal().getId();
            }
            
            Element voorstelling = new Element("voorstelling");
            DateFormat timeformat = new SimpleDateFormat("HH:mm:ss");
            voorstelling.setAttribute(new Attribute("tijd", timeformat.format(v.getTijdstip())));
            voorstelling.setAttribute(new Attribute("film",v.getFilm().getTitel()));
            zaal.addContent(voorstelling);
        }
        
        XMLOutputter xmlOutput = new XMLOutputter();
        xmlOutput.setFormat(Format.getPrettyFormat());
        DateFormat format = new SimpleDateFormat("dd-MM-yyyy");
        xmlOutput.output(doc, new FileWriter("src/XmlSchemas/programmatie-" + format.format(weekbegin) + ".xml"));
        return "File saved to: src/XmlSchemas/programmatie-" + format.format(weekbegin) + ".xml";
        }
        catch (IOException io) {
              return "Bestand kon niet gegenereerd worden.";
        }
    }
}